package com.dormitory_springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory_springboot.entity.Building;
import com.dormitory_springboot.entity.Dormitory;
import com.dormitory_springboot.entity.Student;
import com.dormitory_springboot.entity.Utilities;
import com.dormitory_springboot.form.SearchForm;
import com.dormitory_springboot.mapper.BuildingMapper;
import com.dormitory_springboot.mapper.DormitoryMapper;
import com.dormitory_springboot.mapper.StudentMapper;
import com.dormitory_springboot.mapper.UtilitiesMapper;
import com.dormitory_springboot.service.UtilitiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory_springboot.vo.PageVO;
import com.dormitory_springboot.vo.UtilitiesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-01-08
 */
@Service
public class UtilitiesServiceImpl extends ServiceImpl<UtilitiesMapper, Utilities> implements UtilitiesService {
    @Autowired
    private UtilitiesMapper utilitiesMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public PageVO list(Integer page, Integer size) {
        Page<Utilities> utilitiesPage = new Page<>(page, size);
        Page<Utilities> resultPage = utilitiesMapper.selectPage(utilitiesPage, null);
        List<Utilities> utilitiesList = resultPage.getRecords();
        List<UtilitiesVO> utilitiesVOList = new ArrayList<>();
        for (Utilities utilities : utilitiesList) {
            UtilitiesVO utilitiesVO = new UtilitiesVO();
            BeanUtils.copyProperties(utilities, utilitiesVO);
            // 进行关联查询
            Dormitory dormitory = dormitoryMapper.selectById(utilities.getDormitoryId());
            Building building = buildingMapper.selectById(dormitory.getBuildingId());
            // 设置相应的字段
            utilitiesVO.setDormitoryName(dormitory.getName());
            utilitiesVO.setBuildingId(building.getId());
            utilitiesVO.setBuildingName(building.getName());
            // 计算剩余电费和剩余水量
            BigDecimal remainingWaterAmount = utilities.getPrepaidWaterAmount().subtract(utilities.getActualWaterUsage());
            BigDecimal remainingElectricityAmount = utilities.getPrepaidElectricityAmount().subtract(utilities.getActualElectricityUsage());
            // 设置剩余电费和剩余水量
            utilitiesVO.setRemainingWaterAmount(remainingWaterAmount);
            utilitiesVO.setRemainingElectricityAmount(remainingElectricityAmount);
            utilitiesVOList.add(utilitiesVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(utilitiesVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Utilities> utilitiesPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Utilities> resultPage;
        if (searchForm.getValue().equals("")) {
            resultPage = this.utilitiesMapper.selectPage(utilitiesPage, null);
        } else {
            QueryWrapper<Utilities> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.utilitiesMapper.selectPage(utilitiesPage, queryWrapper);
        }

        List<UtilitiesVO> utilitiesVOList = new ArrayList<>();
        for (Utilities utilities : resultPage.getRecords()) {
            UtilitiesVO utilitiesVO = new UtilitiesVO();
            BeanUtils.copyProperties(utilities, utilitiesVO);
            utilitiesVOList.add(utilitiesVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(utilitiesVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }

    @Override
    public UtilitiesVO getWaterElectricityInfoByStudentId(Integer studentId) {
        // 查询Student信息
        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            // 处理学生不存在的情况
            return null;
        }

        // 查询Dormitory信息
        Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
        if (dormitory == null) {
            // 处理宿舍不存在的情况
            return null;
        }

        // 查询Building信息
        Building building = buildingMapper.selectById(dormitory.getBuildingId());
        if (building == null) {
            // 处理宿舍楼不存在的情况
            return null;
        }

        // 查询Utilities信息
        QueryWrapper<Utilities> wrapper = new QueryWrapper<>();
        wrapper.eq("dormitory_id", dormitory.getId());
        Utilities utilities = utilitiesMapper.selectOne(wrapper);
        if (utilities == null) {
            // 处理水电信息不存在的情况
            return null;
        }

        // 构建UtilitiesVO对象
        UtilitiesVO utilitiesVO = new UtilitiesVO();
        utilitiesVO.setStudentId(student.getId());
        utilitiesVO.setStudentName(student.getName());
        utilitiesVO.setDormitoryId(dormitory.getId());
        utilitiesVO.setDormitoryName(dormitory.getName());
        utilitiesVO.setBuildingId(building.getId());
        utilitiesVO.setBuildingName(building.getName());
        utilitiesVO.setUpdateDate(utilities.getUpdateDate());
        utilitiesVO.setActualElectricityUsage(utilities.getActualElectricityUsage());
        utilitiesVO.setActualWaterUsage(utilities.getActualWaterUsage());
        utilitiesVO.setPrepaidWaterAmount(utilities.getPrepaidWaterAmount());
        utilitiesVO.setPrepaidElectricityAmount(utilities.getPrepaidElectricityAmount());

        BigDecimal remainingElectricityAmount = utilities.getPrepaidElectricityAmount().subtract(utilities.getActualElectricityUsage());
        BigDecimal remainingWaterAmount = utilities.getPrepaidWaterAmount().subtract(utilities.getActualWaterUsage());

        utilitiesVO.setRemainingElectricityAmount(remainingElectricityAmount);
        utilitiesVO.setRemainingWaterAmount(remainingWaterAmount);
        return utilitiesVO;
    }


//    @Override
//    public PageVO search(SearchForm searchForm) {
//        Page<Utilities> utilitiesPage = new Page<>(searchForm.getPage(), searchForm.getSize());
//        Page<Utilities> resultPage;
//        if (searchForm.getValue().equals("")) {
//            resultPage = this.utilitiesMapper.selectPage(utilitiesPage, null);
//        } else {
//            QueryWrapper<Utilities> queryWrapper = new QueryWrapper<>();
//            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
//            resultPage = this.utilitiesMapper.selectPage(utilitiesPage, queryWrapper);
//        }
//        List<UtilitiesVO> utilitiesVOList = new ArrayList<>();
//        for (Utilities utilities : resultPage.getRecords()) {
//            UtilitiesVO utilitiesVO = new UtilitiesVO();
//            BeanUtils.copyProperties(utilities, utilitiesVO);
//            utilitiesVOList.add(utilitiesVO);
//        }
//        PageVO pageVO = new PageVO();
//        pageVO.setData(utilitiesVOList);
//        pageVO.setTotal(resultPage.getTotal());
//        return pageVO;
//    }
}
